﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SASystem.SAServiceRef;

namespace SASystem
{
    public partial class EditVenderFrm : Form
    {
        public EditVenderFrm()
        {
            InitializeComponent();
        }

        private void EditVenderFrm_Load(object sender, EventArgs e)
        {
            DoRefresh();
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) return;

            VendorInfo newVendorInfo = new VendorInfo();
            newVendorInfo.Name = this.textBox_vendorName.Text;
            newVendorInfo.Description = this.richTextBox_vendorDescription.Text;

            bool ret = SABLL.AddVendor(newVendorInfo);

            if (!ret)
            {
                SABLL.ShowErrMsg("添加失败！" + SABLL.GetLastErrMsg());
                return;
            }
            else
            {
                SABLL.ShowInfoMsg("添加成功！");
                DoRefresh();
            }
        }

        private void listView_vendorList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listView_vendorList.SelectedItems == null || this.listView_vendorList.SelectedItems.Count == 0)
            {
                return;
            }
            //get current selected item
            ListViewItem curItem = this.listView_vendorList.SelectedItems[0];
            if (curItem == null)
            {
                return;
            }

            //get vendor info. use vendorName, becuase new added item, the tag is null.
            String vendorName = curItem.Text;
            VendorInfo curVendorInfo = SABLL.GetVendorByName(vendorName);

            if (curVendorInfo == null)
            {
                SABLL.DoAlert(); return;
            }

            this.textBox_vendorName.Text = curVendorInfo.Name;
            this.textBox_vendorName.Tag = curVendorInfo.VendorId;
            this.richTextBox_vendorDescription.Text = curVendorInfo.Description;
        }

        private void button_modify_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) return;

            //get vendorId.
            int vendorId = Convert.ToInt32(this.textBox_vendorName.Tag);
            VendorInfo updateVendorInfo = new VendorInfo();
            updateVendorInfo.VendorId = vendorId;
            updateVendorInfo.Name = this.textBox_vendorName.Text;
            updateVendorInfo.Description = this.richTextBox_vendorDescription.Text;

            bool result = SABLL.UpdateVendor(updateVendorInfo);

            if (!result)
            {
                SABLL.DoAlert(); return;
            }
            else
            {
                SABLL.ShowInfoMsg("修改成功!");
                DoRefresh();
            }
        }

        private void DoRefresh()
        {
            SABLL.InitVendorListView(this.listView_vendorList);
            this.textBox_vendorName.Text = "";
            this.richTextBox_vendorDescription.Text = "";
            this.textBox_vendorName.Tag = null;
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            if (this.listView_vendorList.SelectedItems == null || this.listView_vendorList.SelectedItems.Count == 0)
                return;

            ListViewItem curItem = this.listView_vendorList.SelectedItems[0];

            string vendorName = curItem.Text;

            //check whether there are families for this vendor.
            //
            int vendorId = Convert.ToInt32(this.textBox_vendorName.Tag);
            List<FamilyInfo> famInfos = SABLL.GetFamiliesByVendorId(vendorId);
            if (famInfos != null && famInfos.Count > 0)
            {
                SABLL.ShowInfoMsg(String.Format("无法删除：[{0}]，仍有{1}个零部件属于供应商：[{0}]！", vendorName, famInfos.Count));
                return;
            }

            string msg = String.Format("确定要删除供应商：{0}", vendorName);
            if (SABLL.ShowConfirmMsg(msg) == DialogResult.Yes)
            {
                bool result = SABLL.DeleteVendor(vendorName);
                if (!result)
                {
                    SABLL.DoAlert(); return;
                }
                else
                {
                    SABLL.ShowInfoMsg("删除成功！");
                    DoRefresh();
                }
            }
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool CheckInput()
        {
            if (String.IsNullOrEmpty(this.textBox_vendorName.Text.Trim()))
            {
                SABLL.ShowInfoMsg(@"供应商名字不能为空！");
                return false;
            }
            return true;    
        }
    }
}
